package com.crcode.crm.controller.statistics;

import com.crcode.crm.service.data.EnterpriseService;
import com.crcode.crm.service.data.ProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计控制器
 */
@RequestMapping("/statistics")
@RestController
public class StatisticsController {

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    ProjectService projectService;

    /**
     * 统计企业的数量,根据用户和创建时间,用户可选
     * @param request request
     * @param session session
     * @param uuid 用户的uuid
     * @return Map<String, Object> 包含data和keys的数组
     */
    @RequestMapping(value = "/enterprise", method = RequestMethod.GET)
    public Map<String, Object> aggregationsEntByTime(HttpServletRequest request, HttpSession session, String uuid) {
        List<Map<String, Object>> results = enterpriseService.aggsEntByUserCreateTime(StringUtils.isBlank(uuid) ? null : uuid);
        List<String> keys = new ArrayList<>();
        for (Map<String, Object> result : results) {
            keys.add((String)result.get("name"));
        }
        Map<String, Object> responseJson = new HashMap<>();
        responseJson.put("data", results);
        responseJson.put("keys", keys);
        return responseJson;
    }


    /**
     * 统计项目的数量,根据用户和创建时间,用户可选
     * @param request request
     * @param session session
     * @param uuid 用户的uuid
     * @return Map<String, Object> 包含data和keys的数组
     */
    @RequestMapping(value = "/project", method = RequestMethod.GET)
    public Map<String, Object> aggregationsProByTime(HttpServletRequest request, HttpSession session, String uuid) {
        List<Map<String, Object>> results = projectService.aggsProByUserCreateTime(StringUtils.isBlank(uuid) ? null : uuid);
        List<String> keys = new ArrayList<>();
        for (Map<String, Object> result : results) {
            keys.add((String)result.get("name"));
        }
        Map<String, Object> responseJson = new HashMap<>();
        responseJson.put("data", results);
        responseJson.put("keys", keys);
        return responseJson;
    }
}
